package com.yan.io;

import java.io.*;
import java.nio.charset.IllegalCharsetNameException;

/**
 * @author taoge
 * @Date 2024/8/7 9:26
 * @Description
 */
public class Demo1 {
    public static void main(String[] args) throws IOException {
        File scoureFile = new File("day12_code/src/com/yan");
        File targetFile = new File("day13_code/src/com/yan");
        copyDir(scoureFile ,targetFile);
    }

    public static void copyDir(File scoureFile , File targetFile) throws IOException {
        if(!scoureFile.exists()){
            throw new IllegalArgumentException("源文件夹不存在");
        }
        if(!targetFile.exists()){
            targetFile.mkdirs();
        }
        File[] files = scoureFile.listFiles();
        for (File ziFile : files) {
            if(ziFile.isFile()){
                copyFile(ziFile , new File(targetFile , ziFile.getName()));
            }else if(ziFile.isDirectory()){
                copyDir(ziFile ,new File(targetFile , ziFile.getName()));
            }
        }
    }

    public static void copyFile(File scoureFile , File targetFile) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(scoureFile));
        BufferedWriter bw = new BufferedWriter(new FileWriter(targetFile));
        int len = -1;
        char[] car = new char[1024];
        while((len = br.read(car)) != -1){
            bw.write(car , 0 ,len);
        }

        bw.close();
        br.close();
    }
}
